package com.xm.dao.background;

import com.xm.entity.AfterSales;
import com.xm.entity.Page;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 售后Dao层
 *
 * @author 陈盼
 * @date 2023-12-28 上午 08:29
 */
public class AfterSalesDao extends BaseUtil {
    /**
     * 查询全部售后
     *
     * @return 售后列表
     */
    public List<AfterSales> findAll() {
        // SQL语句
        @Language("SQL")
        String sql = "select * from after_sales order by after_sales_id asc";
        // 执行查询操作
        ResultSet rs = null;
        List<AfterSales> afterSalesList = new ArrayList<>();
        try {
            rs = executeQuery(sql);
            // 遍历结果集
            while (rs.next()) {
                AfterSales afterSales = new AfterSales();
                afterSales.setAfterSalesId(rs.getInt("after_sales_id"));
                afterSales.setAfterSalesType(rs.getInt("after_sales_type"));
                afterSales.setIsInvoice(rs.getInt("is_invoice"));
                afterSales.setInvoiceDate(rs.getDate("invoice_date"));
                afterSales.setInvoiceImages(rs.getString("invoice_images"));
                afterSales.setAfterSalesImages(rs.getString("after_sales_images"));
                afterSales.setAfterSalesCause(rs.getString("after_sales_cause"));
                afterSales.setServiceMode(rs.getInt("service_mode"));
                afterSales.setAfterSalesState(rs.getInt("after_sales_state"));
                afterSales.setAfterSalesDate(rs.getDate("after_sales_date"));
                afterSales.setMaintenanceId(rs.getInt("maintenance_id"));
                afterSales.setCommodityId(rs.getInt("commodity_id"));
                afterSales.setInformationId(rs.getInt("information_id"));
                afterSalesList.add(afterSales);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.close(rs);
        }
        return afterSalesList;
    }

    /**
     * 分页查询
     *
     * @param pageNumber 分页页数
     * @param pageSize   每页数量
     * @return page<对象>
     */
    public Page<AfterSales> pagination(int pageNumber, int pageSize, String afterSalesMc, String afterSalesType, String afterSalesState, String afterSalesDate) {
        Page<AfterSales> page = new Page<>();
        List<AfterSales> list = new ArrayList<>();
        @Language("SQL")
        String countSql = "SELECT COUNT(*) FROM after_sales";
        @Language("SQL")
        String dataSql = "SELECT * FROM after_sales where 1=1";
        if (afterSalesMc != null) {
            if (isNumeric(afterSalesMc)) {
                dataSql += " and after_sales_id like '%" + afterSalesMc + "%'";
            }
        }
        if (afterSalesType != null && afterSalesState != null) {
            dataSql += " and " + afterSalesType + "=after_sales_type and " + afterSalesState + "=after_sales_state";
        }

        String data="0";

        String order = " order by date_entry desc";
        if (afterSalesDate != null) {
            if (data.equals(afterSalesDate)) {
                order = " order by after_sales_date desc";
            } else {
                order = " order by after_sales_date asc";
            }
        }
        dataSql += order;

        dataSql += " LIMIT ?, ?";

        ResultSet countRs = null;
        ResultSet dataRs = null;
        try {
            // 查询总数
            countRs = executeQuery(countSql);
            if (countRs.next()) {
                int count = countRs.getInt(1);
                page.setCount(count);
                page.setTotalPages(pageSize);
            }

            // 分页查询数据
            int offset = (pageNumber - 1) * pageSize;
            dataRs = executeQuery(dataSql, offset, pageSize);
            while (dataRs.next()) {
                AfterSales afterSales = extractAfterSalesFromResultSet(dataRs);
                list.add(afterSales);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(countRs);
            close(dataRs);
        }
        page.setData(list);
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        return page;
    }
    /**
     * 查询detailsStatus的订单数量
     * @param detailsStatus 订单状态
     * @return 订单数量
     * @throws SqlErrorException SQL执行异常
     */
    public int getAfterSalesCountByStatusNine(Integer detailsStatus) throws SqlErrorException {
        @Language("sql")
        String sql = "SELECT COUNT(*) FROM after_sales_details WHERE details_status = ?";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql,detailsStatus);
            if (resultSet.next()) {
                return resultSet.getInt(1);
            } else {
                return 0;
            }
        } catch (Exception e) {
            throw new SqlErrorException("查询失败", e);
        } finally {
            close(resultSet);
        }
    }
    /**
     * 查询的参数
     *
     * @param rs 查询结果集
     * @return 对象
     * @throws SQLException 抛出错误
     */
    private AfterSales extractAfterSalesFromResultSet(ResultSet rs) throws SQLException {
        AfterSales afterSales = new AfterSales();
        afterSales.setAfterSalesId(rs.getInt("after_sales_id"));
        afterSales.setAfterSalesType(rs.getInt("after_sales_type"));
        afterSales.setIsInvoice(rs.getInt("is_invoice"));
        afterSales.setInvoiceDate(rs.getDate("invoice_date"));
        afterSales.setInvoiceImages(rs.getString("invoice_images"));
        afterSales.setAfterSalesImages(rs.getString("after_sales_images"));
        afterSales.setAfterSalesCause(rs.getString("after_sales_cause"));
        afterSales.setServiceMode(rs.getInt("service_mode"));
        afterSales.setAfterSalesState(rs.getInt("after_sales_state"));
        afterSales.setAfterSalesDate(rs.getDate("after_sales_date"));
        afterSales.setMaintenanceId(rs.getInt("maintenance_id"));
        afterSales.setCommodityId(rs.getInt("commodity_id"));
        afterSales.setInformationId(rs.getInt("information_id"));
        return afterSales;
    }

    /**
     * 判断是不是数字
     *
     * @param str 字符串
     * @return turn 是数字，false 不是数字
     */
    public static boolean isNumeric(String str) {
        return str != null && str.matches("-?\\d+(\\.\\d+)?");
    }
}
